/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.easoftware.code.mb;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import net.easoftware.code.beans.ComboItem;
import net.easoftware.code.beans.facade.PerfilSeguridadFc;
import net.easoftware.code.beans.jpa.PerfilSeguridad;
import net.easoftware.code.dao.CatalogosDao;
import net.easoftware.code.dao.PersistenceEjb;
import net.easoftware.code.error.EaError;
import net.easoftware.code.mb.annotation.GenericMaintenanceConfig;
import net.easoftware.code.util.Tools;
import org.apache.commons.beanutils.BeanUtils;

/**
 *
 * @author eliu
 */
@SessionScoped
@Named
@GenericMaintenanceConfig(actions = {GenericMaintenanceConfig.NEW, GenericMaintenanceConfig.DEL}, title = "Mantenimiento de Perfiles Seguridad")
public class MbPerfilesSeguridad implements Serializable, GenericMaintenanceWrapper {

    private List<ComboItem> templates = new LinkedList<>();
    private List<PerfilSeguridadFc> lista = new LinkedList<>();
    @Inject
    private PerfilSeguridadFc bean;
    private PerfilSeguridadFc selected;
    @Inject
    CatalogosDao dao;
    @EJB
    PersistenceEjb ejb;

    public PerfilSeguridadFc getSelected() {
        return selected;
    }

    public void setSelected(PerfilSeguridadFc selected) {
        this.selected = selected;
    }

    public List<ComboItem> getTemplates() {
        if (templates.isEmpty()) {
            templates = Tools.getTemplatesSeguridad();
        }
        return templates;
    }

    public void setTemplates(List<ComboItem> templates) {
        this.templates = templates;
    }

    public List<PerfilSeguridadFc> getLista() {
        if (this.lista.isEmpty()) {
            try {
                lista = Tools.cloneList(PerfilSeguridadFc.class, dao.getAllFromEntity(PerfilSeguridad.class));
            }
            catch (EaError ex) {
                Logger.getLogger(MbPerfilesSeguridad.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return lista;
    }

    public void setLista(List<PerfilSeguridadFc> lista) {
        this.lista = lista;
    }

    @Override
    public void actionSave() throws EaError {
        ejb.guardar(bean.getEntityBean());
        lista.clear();
    }

    @Override
    public void actionUpdate() throws EaError {
        ejb.actualizar(bean.getEntityBean());
        lista.clear();
    }

    @Override
    public void actionDelete() throws EaError {
        ejb.eliminar(bean.getEntityBean());
        lista.clear();
    }

    @Override
    public void actionNew() throws EaError {
        bean.setId(0);
        bean.setNombre("");
    }

    @Override
    public void toExcel() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void toPDF() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getFormPage() {
        return "/module/perfilesseguridad/perfilseguridad.xhtml";
    }

    @Override
    public String getGridPage() {
        return "/module/perfilesseguridad/perfilesseguridad.xhtml";
    }

    @Override
    public void actionSelect() throws EaError {
        try {
            BeanUtils.copyProperties(this.bean, this.selected);
        }
        catch (IllegalAccessException | InvocationTargetException ex) {
            java.util.logging.Logger.getLogger(MbLineasNegocio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void actionCustom1() throws EaError {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void actionCustom2() throws EaError {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getActionCustom1Label() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getActionCustom2Label() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isEnabledActionCustom1() {
        return false;
    }

    @Override
    public boolean isEnabledActionCustom2() {
        return false;
    }
}
